home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / win_m_p / mews11.zip / DISPLAY.C < prev    next >
C/C++ Source or Header  |  1992-10-08  |  45KB  |  1,970 lines

  1. /*
  2.  * The functions in this file handle redisplay. There are two halves, the
  3.  * ones that update the virtual display screen, and the ones that make the
  4.  * physical display screen the same as the virtual display screen. These
  5.  * functions use hints that are left in the windows by the commands.
  6.  *
  7.  */
  8.  
  9. #include    <stdio.h>
  10. #include    "estruct.h"
  11. #include    "eproto.h"
  12. #include    "edef.h"
  13. #include    "elang.h"
  14.  
  15. static VIDEO   **vscreen;               /* Virtual screen. */
  16. #if    MEMMAP == 0
  17. static VIDEO   **pscreen;               /* Physical screen. */
  18. #endif
  19.  
  20. #if     WINDOW_MSWIN
  21. static  int     foulcursor = FALSE;     /* see vtscreen() & movecursor() */
  22. #endif
  23.  
  24. #if     WINDOW_MSWIN
  25. #define MROW    (term.t_nrow)
  26. #define MCOL    (term.t_ncol)
  27. #else
  28. #define MROW    (term.t_mrow)
  29. #defien MCOL    (term.t_mcol)
  30. #endif
  31.  
  32. /*    some local function declarations    */
  33.  
  34. #if    PROTO
  35. #if    MEMMAP
  36. extern PASCAL NEAR updateline(int row, struct VIDEO *vp1);
  37. #else
  38. extern PASCAL NEAR updateline(int row, struct VIDEO *vp1, struct VIDEO *vp2);
  39. #endif
  40. #else
  41. extern PASCAL NEAR updateline();
  42. #endif
  43.  
  44. /*
  45.  * Initialize the data structures used by the display code. The edge vectors
  46.  * used to access the screens are set up. The operating system's terminal I/O
  47.  * channel is set up. All the other things get initialized at compile time.
  48.  * The original window has "WFCHG" set, so that it will get completely
  49.  * redrawn on the first call to "update".
  50.  */
  51.  
  52. PASCAL NEAR vtinit()
  53. {
  54.     register int i;
  55.     register VIDEO *vp;
  56.     int     result;
  57.  
  58.     TTopen();        /* open the screen */
  59.     TTkopen();        /* open the keyboard */
  60.     TTrev(FALSE);
  61.  
  62.     /* allocate the virtual screen pointer array */
  63.     vscreen = (VIDEO **) malloc(MROW*sizeof(VIDEO *));
  64.     if (vscreen == NULL)
  65. #if     WINDOW_MSWIN
  66.                 return FALSE;
  67. #else
  68.         meexit(1);
  69. #endif
  70.  
  71. #if    MEMMAP == 0
  72.     /* allocate the physical shadow screen array */
  73.     pscreen = (VIDEO **) malloc(MROW*sizeof(VIDEO *));
  74.     if (pscreen == NULL)
  75. #if     WINDOW_MSWIN
  76.                 return FALSE;
  77. #else
  78.         meexit(1);
  79. #endif
  80. #endif
  81.  
  82.         result = TRUE;
  83.     /* for every line in the display */
  84.     for (i = 0; i < MROW; ++i) {
  85.  
  86.         /* allocate a virtual screen line */
  87.         vp = (VIDEO *) malloc(sizeof(VIDEO)+MCOL);
  88.         if (vp == NULL) {
  89. #if     WINDOW_MSWIN
  90.                         result = FALSE;
  91. #else
  92.                 meexit(1);
  93. #endif
  94.         }
  95.         else {
  96.             vp->v_flag = 0;        /* init change flags */
  97. #if    COLOR
  98.             vp->v_rfcolor = 7;    /* init fore/background colors */
  99.             vp->v_rbcolor = 0;
  100. #endif
  101.         }
  102.         /* connect virtual line to line array */
  103.         vscreen[i] = vp;
  104.  
  105. #if    MEMMAP == 0
  106.         /* allocate and initialize physical shadow screen line */
  107.         vp = (VIDEO *) malloc(sizeof(VIDEO)+MCOL);
  108.         if (vp == NULL) {
  109. #if     WINDOW_MSWIN
  110.                         result = FALSE;
  111. #else
  112.                 meexit(1);
  113. #endif
  114.         }
  115.         else {
  116.             vp->v_flag = VFNEW;
  117. #if    INSDEL
  118.             vp->v_rline = i;    /* set requested line position */
  119. #endif
  120.         }
  121.         pscreen[i] = vp;
  122. #endif
  123.     }
  124.     return result;;
  125. }
  126.  
  127. #if    CLEAN || WINDOW_MSWIN
  128. /* free up all the dynamically allocated video structures */
  129.  
  130. PASCAL NEAR vtfree()
  131. {
  132.     int i;
  133.     for (i = 0; i < MROW; ++i) {
  134.         if (vscreen && vscreen[i]) free(vscreen[i]);
  135. #if    MEMMAP == 0
  136.         if (pscreen && pscreen[i]) free(pscreen[i]);
  137. #endif
  138.     }
  139.     if (vscreen) free(vscreen);
  140. #if    MEMMAP == 0
  141.     if (pscreen) free(pscreen);
  142. #endif
  143. }
  144. #endif
  145.  
  146. #if     WINDOW_MSWIN
  147. /* vtscreen:    map a screen into the Virtual Terminal system */
  148. /* ========                                                   */
  149.  
  150. PASCAL NEAR vtscreen (SCREEN *sp)
  151. {
  152.     TTflush();
  153.     term.t_roworg = sp->s_roworg;
  154.     term.t_colorg = sp->s_colorg;
  155.     term.t_nrow = sp->s_nrow;
  156.     term.t_ncol = sp->s_ncol;
  157.  
  158.     vscreen = sp->s_virtual;
  159.     pscreen = sp->s_physical;
  160.     term.t_selscr (sp);
  161.     foulcursor = TRUE;
  162. } /* vtscreen */
  163.  
  164. /* vtinitscr: build a virtual terminal resource for a new screen */
  165. /* =========                                                     */
  166.  
  167. int PASCAL NEAR vtinitscr (SCREEN *sp, int nrow, int ncol)
  168.  
  169. /* returns TRUE if successful */
  170. {
  171.     int     result;
  172.  
  173.     if (nrow < 2) nrow = 2;
  174.     term.t_nrow = nrow;
  175.     term.t_ncol = ncol;
  176.     term.t_roworg = 0;
  177.     term.t_colorg = 0;
  178.     if ((result = vtinit ()) == TRUE) {
  179.         sp->s_virtual = vscreen;
  180.         sp->s_physical = pscreen;
  181.         sp->s_nrow = nrow;
  182.         sp->s_ncol = ncol;
  183.         sp->s_roworg = 0;
  184.         sp->s_colorg = 0;
  185.     }
  186.     else {
  187.         vtfree ();
  188.         sp->s_virtual = NULL;
  189.         sp->s_physical = NULL;
  190.     }
  191.     return result;
  192. } /* vtinitscr */
  193.  
  194. /* vtfreescr:  delete a virtual terminal resource for a dying screen */
  195. /* =========                                                         */
  196.  
  197. PASCAL NEAR vtfreescr (SCREEN *sp)
  198. {
  199.     vtscreen (sp);
  200.     vtfree ();
  201.     vtscreen (first_screen);    /* switch to an existing screen */
  202. } /* vtfreescr */
  203.  
  204. /* vtsizescr:   resize the virtual terminal resources */
  205. /* =========                                          */
  206.  
  207. int PASCAL NEAR vtsizescr (SCREEN *sp, int nrow, int ncol)
  208.  
  209. /* returns TRUE if successful. Otherwise, the old VIDEO structures are
  210.    preserved. */
  211. {
  212.     VIDEO   **oldvvp, **oldpvp;
  213.     int     oldnrow, oldncol;
  214.  
  215.     oldvvp = sp->s_virtual;
  216.     oldpvp = sp->s_physical;
  217.     oldnrow = sp->s_nrow;
  218.     oldncol = sp->s_ncol;
  219.     if (vtinitscr (sp, nrow, ncol) == TRUE) {
  220.         /* success! let's free the old VIDEO structures */
  221.         WINDOW  *wp;
  222.         
  223.         vscreen = oldvvp;
  224.         pscreen = oldpvp;
  225.         term.t_nrow = oldnrow;
  226.         term.t_ncol = oldncol;
  227.         vtfree ();      /* get rid of the old VIDEOs (kept up to now in
  228.                case the new allocation had failed) */
  229.         vtscreen (sp);  /* put the new VIDEOs into active duty */
  230.     for (wp = sp->s_first_window; wp != NULL; wp = wp->w_wndp) {
  231.         /* the VIDEOs have been wiped clean. Let's have every window
  232.            marked for a complete redraw */
  233.         wp->w_flag |= WFHARD|WFMODE;
  234.     }
  235.         term.t_sizscr (sp); /* resize the MDI window */
  236.         return TRUE;
  237.     }
  238.     else {
  239.         /* failure! we still need some kind of VIDEO structures, so we
  240.        reuse the old ones */
  241.     term.t_nrow = oldnrow;
  242.     term.t_ncol = oldncol;
  243.     sp->s_virtual = vscreen = oldvvp;
  244.     sp->s_physical = pscreen = oldpvp;
  245.         mlabort (TEXT94);   /* "out of memory" */
  246.         return FALSE;
  247.     }
  248. } /* vtsizescr */
  249. #endif
  250.  
  251. /*
  252.  * Clean up the virtual terminal system, in anticipation for a return to the
  253.  * operating system. Move down to the last line and clear it out (the next
  254.  * system prompt will be written in the line). Shut down the channel to the
  255.  * terminal.
  256.  */
  257. PASCAL NEAR vttidy()
  258. {
  259.     mlerase();
  260.     movecursor(term.t_nrow, 0);
  261.     TTflush();
  262.     TTclose();
  263.     TTkclose();
  264. }
  265.  
  266. /*
  267.  * Set the virtual cursor to the specified row and column on the virtual
  268.  * screen. There is no checking for nonsense values; this might be a good
  269.  * idea during the early stages.
  270.  */
  271. PASCAL NEAR vtmove(row, col)
  272.  
  273. int row, col;
  274.  
  275. {
  276.     vtrow = row;
  277.     vtcol = col;
  278. }
  279.  
  280. /* Write a character to the virtual screen. The virtual row and
  281.    column are updated. If we are not yet on left edge, don't print
  282.    it yet. If the line is too long put a "$" in the last column.
  283.    This routine only puts printing characters into the virtual
  284.    terminal buffers. Only column overflow is checked.
  285. */
  286.  
  287. PASCAL NEAR vtputc(c)
  288.  
  289. int c;
  290.  
  291. {
  292.     register VIDEO *vp;    /* ptr to line being updated */
  293.  
  294.     vp = vscreen[vtrow];
  295.  
  296.     if (c == '\t') {
  297.         do {
  298.             vtputc(' ');
  299.         } while (((vtcol + taboff) % (tabsize)) != 0);
  300.     } else if (vtcol >= term.t_ncol) {
  301.         ++vtcol;
  302.         vp->v_text[term.t_ncol - 1] = '$';
  303.     } else if (disphigh && c > 0x7f) {
  304.         vtputc('^');
  305.         vtputc('!');
  306.         c -= 0x80;
  307.         if (c == '\t') {
  308.             vtputc('^');
  309.             vtputc('I');
  310.         } else
  311.             vtputc(c);
  312.     } else if (c < 0x20 || c == 0x7F) {
  313.         vtputc('^');
  314.         vtputc(c ^ 0x40);
  315.     } else {
  316.         if (vtcol >= 0)
  317.             vp->v_text[vtcol] = c;
  318.         ++vtcol;
  319.     }
  320. }
  321.  
  322. /*
  323.  * Erase from the end of the software cursor to the end of the line on which
  324.  * the software cursor is located.
  325.  */
  326. PASCAL NEAR vteeol()
  327. {
  328.     register VIDEO    *vp;
  329.  
  330.     vp = vscreen[vtrow];
  331.     while (vtcol < term.t_ncol) {
  332.         if (vtcol >= 0)
  333.         vp->v_text[vtcol] = ' ';
  334.     vtcol++;
  335.     }
  336. }
  337.  
  338. /* upscreen:    user routine to force a screen update
  339.         always finishes complete update     */
  340.  
  341. PASCAL NEAR upscreen(f, n)
  342.  
  343. int f,n;    /* prefix flag and argument */
  344.  
  345. {
  346.     update(TRUE);
  347.     return(TRUE);
  348. }
  349.  
  350. /*
  351.  * Make sure that the display is right. This is a three part process. First,
  352.  * scan through all of the windows looking for dirty ones. Check the framing,
  353.  * and refresh the screen. Second, make sure that "currow" and "curcol" are
  354.  * correct for the current window. Third, make the virtual and physical
  355.  * screens the same.
  356.  */
  357. PASCAL NEAR update(force)
  358.  
  359. int force;    /* force update past type ahead? */
  360.  
  361. {
  362.     register WINDOW *wp;
  363. #if     WINDOW_MSWIN
  364.     SCREEN  *sp;
  365. #endif
  366. #if    TYPEAH
  367.     if (force == FALSE && typahead())
  368.         return(TRUE);
  369. #endif
  370. #if    VISMAC == 0
  371.     if (force == FALSE && kbdmode == PLAY)
  372.         return(TRUE);
  373. #endif
  374.  
  375. #if     WINDOW_MSWIN
  376.         if (force == FALSE) {
  377.             defferupdate = TRUE;
  378.             return TRUE;
  379.         }
  380.         else defferupdate = FALSE;
  381.  
  382.     /* loop through all screens to update even partially hidden ones.
  383.        Note that we process the "first" screen last */
  384.     sp = first_screen;
  385.     do {
  386.         char scroll_flag = 0;
  387.         int  scroll_fcol;
  388.         static WINDOW *scroll_wp = (WINDOW *)NULL;
  389.         
  390.         sp = sp->s_next_screen;
  391.         if (sp == (SCREEN *)NULL) {
  392.             sp = first_screen;
  393.             sp->s_cur_window = curwp;   /* not always up to date */
  394.         }
  395.         vtscreen (sp);
  396.         wheadp = sp->s_first_window;
  397.         scroll_fcol = sp->s_cur_window->w_fcol;
  398. #endif
  399.  
  400.     /* update any windows that need refreshing */
  401.     wp = wheadp;
  402.     while (wp != NULL) {
  403.         if (wp->w_flag) {
  404.             /* if the window has changed, service it */
  405.             reframe(wp);    /* check the framing */
  406.             if ((wp->w_flag & ~WFMODE) == WFEDIT)
  407.                 updone(wp);    /* update EDITed line */
  408.             else if (wp->w_flag & ~WFMOVE)
  409.                 updall(wp);    /* update all lines */
  410.             if (wp->w_flag & WFMODE)
  411.                 modeline(wp);    /* update modeline */
  412. #if WINDOW_MSWIN
  413.                         if (wp == sp->s_cur_window) {
  414.                             if (wp != scroll_wp) {
  415.                                 /* switched to another window, force update
  416.                                    of both scroll bars */
  417.                                 scroll_flag = WFHARD;
  418.                                 scroll_wp = wp;
  419.                             }
  420.                             else scroll_flag = wp->w_flag & WFHARD;
  421.                         }
  422. #endif
  423.             wp->w_flag = 0;
  424.             wp->w_force = 0;
  425.         }
  426.  
  427. #if    1    /* take this out before the release! */
  428.     if (wp->w_wndp == wheadp) {    /* erroneously circular list */
  429.         wp->w_wndp = (WINDOW *)NULL;
  430.         mlwrite("DAN!!! a bogus circular window list!!!");
  431.         TTgetc();
  432.     }
  433. #endif
  434.         /* on to the next window */
  435.         wp = wp->w_wndp;
  436.     }
  437.  
  438.     /* recalc the current hardware cursor location */
  439. #if     WINDOW_MSWIN
  440.     if (sp == first_screen) {
  441.     updpos();
  442.     }
  443. #else
  444.     updpos();
  445. #endif
  446.  
  447. #if    MEMMAP
  448.     /* update the cursor and flush the buffers */
  449.     movecursor(currow, curcol - lbound);
  450. #endif
  451.  
  452.     /* check for lines to de-extend */
  453.     upddex();
  454.  
  455.     /* if screen is garbage, re-plot it */
  456.     if (sgarbf != FALSE)
  457.         if (gflags & GFSDRAW)
  458.             sgarbf = FALSE;
  459.         else
  460.             updgar();
  461.  
  462.     /* update the virtual screen to the physical screen */
  463.     updupd(force);
  464. #if     WINDOW_MSWIN
  465.         if (scroll_fcol != sp->s_cur_window->w_fcol) {
  466.             scroll_flag |= WFMOVE;
  467.         }
  468.         if (scroll_flag) updscrollbars(sp, scroll_flag);
  469.     } while (sp != first_screen);
  470.     sgarbf = FALSE;
  471. #endif
  472.  
  473.     /* update the cursor and flush the buffers */
  474.     movecursor(currow, curcol - lbound);
  475.     TTflush();
  476.  
  477.     return(TRUE);
  478. }
  479.  
  480. /*    reframe:    check to see if the cursor is on in the window
  481.             and re-frame it if needed or wanted        */
  482.  
  483. PASCAL NEAR reframe(wp)
  484.  
  485. WINDOW *wp;
  486.  
  487. {
  488.     register LINE *lp;    /* search pointer */
  489.     register LINE *rp;    /* reverse search pointer */
  490.     register LINE *hp;    /* ptr to header line in buffer */
  491.     register LINE *tp;    /* temp debugging pointer */
  492.     register int i;        /* general index/# lines to scroll */
  493.     register int nlines;    /* number of lines in current window */
  494.  
  495.     /* figure out our window size */
  496.     nlines = wp->w_ntrows;
  497.     if (modeflag == FALSE)
  498.         nlines++;
  499.  
  500.     /* if not a requested reframe, check for a needed one */
  501.     if ((wp->w_flag & WFFORCE) == 0) {
  502.         lp = wp->w_linep;
  503.         for (i = 0; i < nlines; i++) {
  504.  
  505.             /* if the line is in the window, no reframe */
  506.             if (lp == wp->w_dotp)
  507.                 return(TRUE);
  508.  
  509.             /* if we are at the end of the file, reframe */
  510.             if (lp == wp->w_bufp->b_linep)
  511.                 break;
  512.  
  513.             /* on to the next line */
  514.             lp = lforw(lp);
  515.         }
  516.     }
  517.  
  518.     /* reaching here, we need a window refresh */
  519.     i = wp->w_force;
  520.  
  521.     /* if smooth scrolling is enabled,
  522.         first.. have we gone off the top? */
  523.     if (sscroll && ((wp->w_flag & WFFORCE) == 0)) {
  524.         /* search thru the buffer looking for the point */
  525.         tp = lp = rp = wp->w_linep;
  526.         hp = wp->w_bufp->b_linep;
  527.         while ((lp != hp) || (rp != hp)) {
  528.  
  529.             /* did we scroll downward? */
  530.             if (lp == wp->w_dotp) {
  531.                 i = nlines - 1;
  532.                 break;
  533.             }
  534.  
  535.             /* did we scroll upward? */
  536.             if (rp == wp->w_dotp) {
  537.                 i = 0;
  538.                 break;
  539.             }
  540.  
  541.             /* advance forward and back */
  542.             if (lp != hp)
  543.                 lp = lforw(lp);
  544.             if (rp != hp)
  545.                 rp = lback(rp);
  546.  
  547.             /* problems????? */
  548.             if (lp == tp || rp == tp) {
  549.                 mlforce("BUG IN SMOOTH SCROLL--GET DAN!\n");
  550.                 TTgetc();
  551.             }
  552.         }
  553.     /* how far back to reframe? */
  554.     } else if (i > 0) {    /* only one screen worth of lines max */
  555.         if (--i >= nlines)
  556.             i = nlines - 1;
  557.     } else if (i < 0) {    /* negative update???? */
  558.         i += nlines;
  559.         if (i < 0)
  560.             i = 0;
  561.     } else
  562.         i = nlines / 2;
  563.  
  564.     /* backup to new line at top of window */
  565.     lp = wp->w_dotp;
  566.     while (i != 0 && lback(lp) != wp->w_bufp->b_linep) {
  567.         --i;
  568.         if (i < 0) {
  569.             mlforce("OTHER BUG IN DISPLAY --- GET DAN!!!\n");
  570.             TTgetc();
  571.         }
  572.         lp = lback(lp);
  573.     }
  574.  
  575.     /* and reset the current line at top of window */
  576.     wp->w_linep = lp;
  577.     wp->w_flag |= WFHARD;
  578.     wp->w_flag &= ~WFFORCE;
  579.     return(TRUE);
  580. }
  581.  
  582. /*    updone: update the current line to the virtual screen        */
  583.  
  584. PASCAL NEAR updone(wp)
  585.  
  586. WINDOW *wp;    /* window to update current line in */
  587.  
  588. {
  589.     register LINE *lp;    /* line to update */
  590.     register int sline;    /* physical screen line to update */
  591.     register int i;
  592.  
  593.     /* search down the line we want */
  594.     lp = wp->w_linep;
  595.     sline = wp->w_toprow;
  596.     while (lp != wp->w_dotp) {
  597.         ++sline;
  598.         lp = lforw(lp);
  599.     }
  600.  
  601.     /* and update the virtual line */
  602.     vscreen[sline]->v_flag |= VFCHG;
  603.     vscreen[sline]->v_flag &= ~VFREQ;
  604.     taboff = wp->w_fcol;
  605.     vtmove(sline, -taboff);
  606.     for (i=0; i < llength(lp); ++i)
  607.         vtputc(lgetc(lp, i));
  608. #if    COLOR
  609.     vscreen[sline]->v_rfcolor = wp->w_fcolor;
  610.     vscreen[sline]->v_rbcolor = wp->w_bcolor;
  611. #endif
  612.     vteeol();
  613.     taboff = 0;
  614. }
  615.  
  616. /*    updall: update all the lines in a window on the virtual screen */
  617.  
  618. PASCAL NEAR updall(wp)
  619.  
  620. WINDOW *wp;    /* window to update lines in */
  621.  
  622. {
  623.     register LINE *lp;    /* line to update */
  624.     register int sline;    /* physical screen line to update */
  625.     register int i;
  626.     register int nlines;    /* number of lines in the current window */
  627.  
  628.     /* search down the lines, updating them */
  629.     lp = wp->w_linep;
  630.     sline = wp->w_toprow;
  631.     nlines = wp->w_ntrows;
  632.     if (modeflag == FALSE)
  633.         nlines++;
  634.     taboff = wp->w_fcol;
  635.     while (sline < wp->w_toprow + nlines) {
  636.  
  637.         /* and update the virtual line */
  638.         vscreen[sline]->v_flag |= VFCHG;
  639.         vscreen[sline]->v_flag &= ~VFREQ;
  640.         vtmove(sline, -taboff);
  641.         if (lp != wp->w_bufp->b_linep) {
  642.             /* if we are not at the end */
  643.             for (i=0; i < llength(lp); ++i)
  644.                 vtputc(lgetc(lp, i));
  645.             lp = lforw(lp);
  646.         }
  647.  
  648.         /* make sure we are on screen */
  649.         if (vtcol < 0)
  650.             vtcol = 0;
  651.  
  652.         /* on to the next one */
  653. #if    COLOR
  654.         vscreen[sline]->v_rfcolor = wp->w_fcolor;
  655.         vscreen[sline]->v_rbcolor = wp->w_bcolor;
  656. #endif
  657.         vteeol();
  658.         ++sline;
  659.     }
  660.     taboff = 0;
  661. }
  662.  
  663. /*    updpos: update the position of the hardware cursor and handle extended
  664.         lines. This is the only update for simple moves.    */
  665.  
  666. PASCAL NEAR updpos()
  667.  
  668. {
  669.     register LINE *lp;
  670.     register int c;
  671.     register int i;
  672.  
  673.     /* find the current row */
  674.     lp = curwp->w_linep;
  675.     currow = curwp->w_toprow;
  676.     while (lp != curwp->w_dotp) {
  677.         ++currow;
  678.         lp = lforw(lp);
  679.     }
  680.  
  681.     /* find the current column */
  682.     curcol = 0;
  683.     i = 0;
  684.     while (i < curwp->w_doto) {
  685.         c = lgetc(lp, i++);
  686.         if (c == '\t')
  687.             curcol += - (curcol % tabsize) + (tabsize - 1);
  688.         else {
  689.             if (disphigh && c > 0x7f) {
  690.                 curcol += 2;
  691.                 c -= 0x80;
  692.             }
  693.             if (c < 0x20 || c == 0x7f)
  694.                 ++curcol;
  695.         }
  696.         ++curcol;
  697.     }
  698.  
  699.     /* adjust by the current first column position */
  700.     curcol -= curwp->w_fcol;
  701.  
  702.     /* make sure it is not off the left side of the screen */
  703.     while (curcol < 0) {
  704.         if (curwp->w_fcol >= hjump) {
  705.             curcol += hjump;
  706.             curwp->w_fcol -= hjump;
  707.         } else {
  708.             curcol += curwp->w_fcol;
  709.             curwp->w_fcol = 0;
  710.         }
  711.         curwp->w_flag |= WFHARD | WFMODE;
  712.     }
  713.  
  714.     /* if horizontall scrolling is enabled, shift if needed */
  715.     if (hscroll) {
  716.         while (curcol >= term.t_ncol - 1) {
  717.             curcol -= hjump;
  718.             curwp->w_fcol += hjump;
  719.             curwp->w_flag |= WFHARD | WFMODE;
  720.         }
  721.     } else {
  722.     /* if extended, flag so and update the virtual line image */
  723.         if (curcol >=  term.t_ncol - 1) {
  724.             vscreen[currow]->v_flag |= (VFEXT | VFCHG);
  725.             updext();
  726.         } else
  727.             lbound = 0;
  728.     }
  729.  
  730.     /* update the current window if we have to move it around */
  731.     if (curwp->w_flag & WFHARD)
  732.         updall(curwp);
  733.     if (curwp->w_flag & WFMODE)
  734.         modeline(curwp);
  735.     curwp->w_flag = 0;
  736. }
  737.  
  738. /*    upddex: de-extend any line that derserves it        */
  739.  
  740. PASCAL NEAR upddex()
  741.  
  742. {
  743.     register WINDOW *wp;
  744.     register LINE *lp;
  745.     register int i,j;
  746.     register int nlines;    /* number of lines in the current window */
  747.  
  748.     wp = wheadp;
  749.  
  750.     while (wp != NULL) {
  751.         lp = wp->w_linep;
  752.         i = wp->w_toprow;
  753.         nlines = wp->w_ntrows;
  754.         if (modeflag == FALSE)
  755.             nlines++;
  756.  
  757.         while (i < wp->w_toprow + nlines) {
  758.             if (vscreen[i]->v_flag & VFEXT) {
  759.                 if ((wp != curwp) || (lp != wp->w_dotp) ||
  760.                    (curcol < term.t_ncol - 1)) {
  761.                     taboff = wp->w_fcol;
  762.                     vtmove(i, -taboff);
  763.                     for (j = 0; j < llength(lp); ++j)
  764.                         vtputc(lgetc(lp, j));
  765.                     vteeol();
  766.                     taboff = 0;
  767.  
  768.                     /* this line no longer is extended */
  769.                     vscreen[i]->v_flag &= ~VFEXT;
  770.                     vscreen[i]->v_flag |= VFCHG;
  771.                 }
  772.             }
  773.             lp = lforw(lp);
  774.             ++i;
  775.         }
  776.         /* and onward to the next window */
  777.         wp = wp->w_wndp;
  778.     }
  779. }
  780.  
  781. /*    updgar: if the screen is garbage, clear the physical screen and
  782.         the virtual screen and force a full update        */
  783.  
  784. PASCAL NEAR updgar()
  785.  
  786. {
  787.     register int i;
  788. #if    MEMMAP == 0
  789.     register int j;
  790.     register char *txt;
  791. #endif
  792.  
  793.     for (i = 0; i < term.t_nrow; ++i) {
  794.         vscreen[i]->v_flag |= VFCHG;
  795. #if    REVSTA
  796.         vscreen[i]->v_flag &= ~VFREV;
  797. #endif
  798. #if    COLOR
  799.         vscreen[i]->v_fcolor = gfcolor;
  800.         vscreen[i]->v_bcolor = gbcolor;
  801. #endif
  802. #if    MEMMAP == 0
  803.         txt = pscreen[i]->v_text;
  804.         for (j = 0; j < term.t_ncol; ++j)
  805.             txt[j] = ' ';
  806. #endif
  807.     }
  808.  
  809.     movecursor(0, 0);         /* Erase the screen. */
  810. #if     COLOR && WINDOW_MSWIN
  811.         TTforg (gfcolor);   /* inform the driver of the colors */
  812.         TTbacg (gbcolor);   /* to be used for erase to end of page */
  813. #endif
  814. #if     REVSTA && WINDOW_MSWIN
  815.         TTrev (FALSE);
  816. #endif
  817.     (*term.t_eeop)();
  818. #if     !WINDOW_MSWIN
  819.     sgarbf = FALSE;          /* Erase-page clears */
  820.     mpresf = FALSE;          /* the message area. */
  821. #endif
  822. #if    COLOR
  823.     mlerase();            /* needs to be cleared if colored */
  824. #if     WINDOW_MSWIN
  825.     mpresf = FALSE;
  826. #endif
  827. #endif
  828. }
  829.  
  830. #if     !WINDOW_MSWIN
  831. /*    for simple screen size changes (no window re-allocation involved)
  832.     do the following things
  833. */
  834.  
  835. PASCAL NEAR update_size()
  836.  
  837. {
  838.     /* if we need the size update */
  839.     if ((first_screen->s_roworg != term.t_roworg) |
  840.         (first_screen->s_colorg != term.t_colorg) |
  841.         (first_screen->s_nrow != term.t_nrow) |
  842.         (first_screen->s_ncol != term.t_ncol)) {
  843.  
  844.         /* reset the terminal drivers size concept */
  845.         term.t_roworg = first_screen->s_roworg;
  846.         term.t_colorg = first_screen->s_colorg;
  847.         term.t_nrow = first_screen->s_nrow;
  848.         term.t_ncol = first_screen->s_ncol;
  849.  
  850.         /* make sure the update routines know we need a full update */
  851.         sgarbf = TRUE;
  852.     }
  853. }
  854. #endif
  855.  
  856. /*    Display a pop up window.  Page it for the user.  Any key other
  857.     than a space gets pushed back into the input stream to be interpeted
  858.     later as a command.
  859. */
  860.  
  861. #if    PROTO
  862. int PASCAL NEAR pop(BUFFER *popbuf)
  863. #else
  864. int PASCAL NEAR pop(popbuf)
  865.  
  866. BUFFER *popbuf;
  867. #endif
  868. {
  869.     register int index;    /* index into the current output line */
  870.     register int llen;    /* length of the current output line */
  871.     register int cline;    /* current screen line number */
  872.     LINE *lp;    /* ptr to next line to display */
  873.     int numlines;    /* remaining number of lines to display */
  874.     int c;        /* input character */
  875.  
  876.     /* add the barrior line to the end of the pop up buffer */
  877.     addline(popbuf, "------------------------------------------");
  878.  
  879.     /* set up to scan pop up buffer */
  880.     lp = lforw(popbuf->b_linep);
  881. /*    mlerase();    do we really need this? */
  882.     numlines = term.t_nrow-2;
  883.     cline = 0;
  884.  
  885.     while (lp != popbuf->b_linep) {
  886.  
  887.         /* update the virtual screen image for this one line */
  888.         vtmove(cline, 0);
  889.         llen = llength(lp);
  890.         for (index = 0; index < llen; index++)
  891.             vtputc(lp->l_text[index]);
  892.         vteeol();
  893. #if    COLOR
  894.         vscreen[cline]->v_rfcolor = gfcolor;
  895.         vscreen[cline]->v_rbcolor = gbcolor;
  896. #endif
  897.         vscreen[cline]->v_flag &= ~VFREQ;
  898.         vscreen[cline++]->v_flag |= VFCHG|VFCOL;
  899.  
  900.         if (numlines-- < 1) {
  901.  
  902.             /* update the virtual screen to the physical screen */
  903.             updupd(FALSE);
  904.  
  905.             /* tell the user there is more */
  906.             mlwrite("--- more ---");
  907.             TTflush();
  908.  
  909.             /* and see if they want more */
  910.             if ((c = tgetc()) != ' ') {
  911.                 cpending = TRUE;
  912.                 charpending = c;
  913.                 upwind();
  914.                 return(TRUE);
  915.             }
  916.  
  917.             /* reset the line counters */
  918.             numlines = term.t_nrow-2;
  919.             cline = 0;
  920.         }
  921.  
  922.         /* on to the next line */
  923.         lp = lforw(lp);
  924.     }
  925.     if (numlines > 0) {
  926.  
  927.         /* update the virtual screen to the physical screen */
  928.         updupd(FALSE);
  929.         TTflush();
  930.  
  931.         if ((c = tgetc()) != ' ') {
  932.             cpending = TRUE;
  933.             charpending = c;
  934.         }
  935.     }
  936.     upwind();
  937.     return(TRUE);
  938. }
  939.  
  940. /*    updupd: update the physical screen from the virtual screen    */
  941.  
  942. PASCAL NEAR updupd(force)
  943.  
  944. int force;    /* forced update flag */
  945.  
  946. {
  947.     register VIDEO *vp1;
  948.     register int i;
  949.  
  950.     for (i = 0; i < term.t_nrow; ++i) {
  951.         vp1 = vscreen[i];
  952.  
  953.         /* for each line that needs to be updated*/
  954.         if ((vp1->v_flag & VFCHG) != 0) {
  955. #if    TYPEAH
  956.             if (force == FALSE && typahead())
  957.                 return(TRUE);
  958. #endif
  959. #if    MEMMAP
  960.             updateline(i, vp1);
  961. #else
  962.             updateline(i, vp1, pscreen[i]);
  963. #endif
  964.         }
  965.     }
  966.     return(TRUE);
  967. }
  968.  
  969. /*    updext: update the extended line which the cursor is currently
  970.         on at a column greater than the terminal width. The line
  971.         will be scrolled right or left to let the user see where
  972.         the cursor is
  973.                                 */
  974. PASCAL NEAR updext()
  975.  
  976. {
  977.     register int rcursor;    /* real cursor location */
  978.     register LINE *lp;    /* pointer to current line */
  979.     register int j;     /* index into line */
  980.  
  981.     /* calculate what column the real cursor will end up in */
  982.     rcursor = ((curcol - term.t_ncol) % term.t_scrsiz)
  983.             + term.t_margin;
  984.     lbound = curcol - rcursor + 1;
  985.     taboff = lbound + curwp->w_fcol;
  986.  
  987.     /* scan through the line outputing characters to the virtual screen */
  988.     /* once we reach the left edge                    */
  989.     vtmove(currow, -taboff); /* start scanning offscreen */
  990.     lp = curwp->w_dotp;        /* line to output */
  991.     for (j=0; j<llength(lp); ++j)    /* until the end-of-line */
  992.         vtputc(lgetc(lp, j));
  993.  
  994.     /* truncate the virtual line, restore tab offset */
  995.     vteeol();
  996.     taboff = 0;
  997.  
  998.     /* and put a '$' in column 1 */
  999.     vscreen[currow]->v_text[0] = '$';
  1000. }
  1001.  
  1002. /*
  1003.  * Update a single line. This does not know how to use insert or delete
  1004.  * character sequences; we are using VT52 functionality. Update the physical
  1005.  * row and column variables. It does try an exploit erase to end of line.
  1006.  */
  1007. #if    MEMMAP
  1008. /*    UPDATELINE specific code for memory mapped displays */
  1009.  
  1010. PASCAL NEAR updateline(row, vp)
  1011.  
  1012. int row;        /* row of screen to update */
  1013. struct VIDEO *vp;    /* virtual screen image */
  1014.  
  1015. {
  1016.     if (vp->v_flag & VFREQ)
  1017.         TTrev(TRUE);
  1018. #if    COLOR
  1019.     scwrite(row, vp->v_text, vp->v_rfcolor, vp->v_rbcolor);
  1020.     vp->v_fcolor = vp->v_rfcolor;
  1021.     vp->v_bcolor = vp->v_rbcolor;
  1022. #else
  1023.     scwrite(row, vp->v_text, 7, 0);
  1024. #endif
  1025.     if (vp->v_flag & VFREQ)
  1026.         TTrev(FALSE);
  1027.     vp->v_flag &= ~(VFCHG | VFCOL);    /* flag this line as changed */
  1028.  
  1029. }
  1030.  
  1031. #else
  1032. PASCAL NEAR updateline(row, vp, pp)
  1033.  
  1034. int row;        /* row of screen to update */
  1035. struct VIDEO *vp;    /* virtual screen image */
  1036. struct VIDEO *pp;    /* physical screen image */
  1037.  
  1038. {
  1039.  
  1040.     register char *cp1;
  1041.     register char *cp2;
  1042.     register char *cp3;
  1043.     register char *cp4;
  1044.     register char *cp5;
  1045.     register int nbflag;    /* non-blanks to the right flag? */
  1046.     int rev;        /* reverse video flag */
  1047.     int req;        /* reverse video request flag */
  1048.     int upcol;        /* update column (KRS) */
  1049.  
  1050.     /* set up pointers to virtual and physical lines */
  1051.     cp1 = &vp->v_text[0];
  1052.     cp2 = &pp->v_text[0];
  1053.  
  1054. #if    COLOR
  1055.     TTforg(vp->v_rfcolor);
  1056.     TTbacg(vp->v_rbcolor);
  1057. #endif
  1058.  
  1059. #if    REVSTA | COLOR
  1060.     /* if we need to change the reverse video status of the
  1061.        current line, we need to re-write the entire line     */
  1062.     rev = (vp->v_flag & VFREV) == VFREV;
  1063.     req = (vp->v_flag & VFREQ) == VFREQ;
  1064.     if ((rev != req)
  1065. #if    COLOR
  1066.         || (vp->v_fcolor != vp->v_rfcolor) || (vp->v_bcolor != vp->v_rbcolor)
  1067. #endif
  1068. #if    HP150
  1069.     /* the HP150 has some reverse video problems */
  1070.         || req || rev
  1071. #endif
  1072.             ) {
  1073.         movecursor(row, 0);    /* Go to start of line. */
  1074.         /* set rev video if needed */
  1075.         if (rev != req)
  1076.             (*term.t_rev)(req);
  1077.  
  1078.         /* scan through the line and dump it to the screen and
  1079.            the virtual screen array                */
  1080.         cp3 = &vp->v_text[term.t_ncol];
  1081.         while (cp1 < cp3) {
  1082.             TTputc(*cp1);
  1083.             ++ttcol;
  1084.             *cp2++ = *cp1++;
  1085.         }
  1086.         /* turn rev video off */
  1087.         if (rev != req)
  1088.             (*term.t_rev)(FALSE);
  1089.  
  1090.         /* update the needed flags */
  1091.         vp->v_flag &= ~VFCHG;
  1092.         if (req)
  1093.             vp->v_flag |= VFREV;
  1094.         else
  1095.             vp->v_flag &= ~VFREV;
  1096. #if    COLOR
  1097.         vp->v_fcolor = vp->v_rfcolor;
  1098.         vp->v_bcolor = vp->v_rbcolor;
  1099. #endif
  1100.         pp->v_flag &= ~VFNEW;
  1101.         return(TRUE);
  1102.     }
  1103. #endif
  1104.  
  1105.     upcol = 0;
  1106.  
  1107.     if (!(pp->v_flag & VFNEW)) {
  1108.     /* advance past any common chars at the left */
  1109.     while (cp1 != &vp->v_text[term.t_ncol] && cp1[0] == cp2[0]) {
  1110.         ++cp1;
  1111.         ++upcol;
  1112.         ++cp2;
  1113.     }
  1114.  
  1115. #if    DBCS
  1116.     /* don't optimize on the left in the middle of a 2 byte char */
  1117.     if ((cp1 > &vp->v_text[0]) && is2byte(vp->v_text, cp1 - 1)) {
  1118.         --cp1;
  1119.         --upcol;
  1120.         --cp2;
  1121.     }
  1122. #endif
  1123.  
  1124.  
  1125. /* This can still happen, even though we only call this routine on changed
  1126.  * lines. A hard update is always done when a line splits, a massive
  1127.  * change is done, or a buffer is displayed twice. This optimizes out most
  1128.  * of the excess updating. A lot of computes are used, but these tend to
  1129.  * be hard operations that do a lot of update, so I don't really care.
  1130.  */
  1131.     /* if both lines are the same, no update needs to be done */
  1132.     if (cp1 == &vp->v_text[term.t_ncol]) {
  1133.         vp->v_flag &= ~VFCHG;        /* flag this line is changed */
  1134.         return(TRUE);
  1135.     }
  1136.     }
  1137.     /* find out if there is a match on the right */
  1138.     nbflag = FALSE;
  1139.     cp3 = &vp->v_text[term.t_ncol];
  1140.     cp4 = &pp->v_text[term.t_ncol];
  1141.  
  1142.     if (!(pp->v_flag & VFNEW)) {
  1143.     while (cp3[-1] == cp4[-1]) {
  1144.         --cp3;
  1145.         --cp4;
  1146.         if (cp3[0] != ' ')        /* Note if any nonblank */
  1147.             nbflag = TRUE;        /* in right match. */
  1148.     }
  1149.  
  1150. #if    DBCS
  1151.     /* don't stop in the middle of a 2 byte char */
  1152.     if (is2byte(vp->v_text, cp3-1) || is2byte(pp->v_text, cp4-1)) {
  1153.         ++cp3;
  1154.         ++cp4;
  1155.     }
  1156. #endif
  1157.     }
  1158.     cp5 = cp3;
  1159.  
  1160.     /* Erase to EOL ? */
  1161.     if (nbflag == FALSE && eolexist == TRUE && (req != TRUE)) {
  1162.         while (cp5!=cp1 && cp5[-1]==' ')
  1163.             --cp5;
  1164.  
  1165.         if (cp3-cp5 <= 3)        /* Use only if erase is */
  1166.             cp5 = cp3;        /* fewer characters. */
  1167.     }
  1168.  
  1169.     /* move to the begining of the text to update */
  1170.     movecursor(row, upcol);
  1171.  
  1172. #if    REVSTA
  1173.     if (rev)
  1174.         TTrev(TRUE);
  1175. #endif
  1176.  
  1177.     while (cp1 != cp5) {        /* Ordinary. */
  1178.         TTputc(*cp1);
  1179.         ++ttcol;
  1180.         *cp2++ = *cp1++;
  1181.     }
  1182.  
  1183.     if (cp5 != cp3) {        /* Erase. */
  1184.         TTeeol();
  1185.         while (cp1 != cp3)
  1186.             *cp2++ = *cp1++;
  1187.     }
  1188. #if    REVSTA
  1189.     if (rev)
  1190.         TTrev(FALSE);
  1191. #endif
  1192.     vp->v_flag &= ~VFCHG;       /* flag this line as updated */
  1193.     pp->v_flag &= ~VFNEW;
  1194.     return(TRUE);
  1195. }
  1196. #endif
  1197.  
  1198. /*
  1199.  * Redisplay the mode line for the window pointed to by the "wp". This is the
  1200.  * only routine that has any idea of how the modeline is formatted. You can
  1201.  * change the modeline format by hacking at this routine. Called by "update"
  1202.  * any time there is a dirty window.
  1203.  */
  1204. PASCAL NEAR modeline(wp)
  1205.  
  1206. WINDOW *wp;    /* window to update modeline for */
  1207.  
  1208. {
  1209.     register char *cp;
  1210.     register int c;
  1211.     register int n;        /* cursor position count */
  1212.     register BUFFER *bp;
  1213.     register int i;        /* loop index */
  1214.     register int lchar;     /* character to draw line in buffer with */
  1215.     register int firstm;    /* is this the first mode? */
  1216.     char tline[NLINE];    /* buffer for part of mode line */
  1217.     char time[6];        /* to hold current time */
  1218.  
  1219.     /* don't bother if there is none! */
  1220.     if (modeflag == FALSE)
  1221.         return;
  1222.  
  1223.     n = wp->w_toprow+wp->w_ntrows;        /* Location. */
  1224.  
  1225. /*
  1226.     Note that we assume that setting REVERSE will cause the terminal
  1227.     driver to draw with the inverted relationship of fcolor and
  1228.     bcolor, so that when we say to set the foreground color to "white"
  1229.     and background color to "black", the fact that "reverse" is
  1230.     enabled means that the terminal driver actually draws "black" on a
  1231.     background of "white".  Makes sense, no?  This way, devices for
  1232.     which the color controls are optional will still get the "reverse"
  1233.     signals.
  1234. */
  1235.  
  1236.     vscreen[n]->v_flag |= VFCHG | VFREQ | VFCOL;    /* Redraw next time. */
  1237. #if    COLOR
  1238.     vscreen[n]->v_rfcolor = 7;            /* black on */
  1239.     vscreen[n]->v_rbcolor = 0;            /* white.....*/
  1240. #endif
  1241.     vtmove(n, 0);                /* Seek to right line. */
  1242.     if (wp == curwp)            /* mark the current buffer */
  1243.         lchar = '=';
  1244.     else
  1245. #if    REVSTA
  1246.     if (revexist)
  1247.         lchar = ' ';
  1248.     else
  1249. #endif
  1250.         lchar = '-';
  1251.  
  1252.     bp = wp->w_bufp;
  1253.     if ((bp->b_flag&BFTRUNC) != 0)        /* "#" if truncated */
  1254.         vtputc('#');
  1255.     else
  1256.         vtputc(lchar);
  1257.  
  1258.     if ((bp->b_flag&BFCHG) != 0)        /* "*" if changed. */
  1259.         vtputc('*');
  1260.     else
  1261.         vtputc(lchar);
  1262.  
  1263.     if ((bp->b_flag&BFNAROW) != 0) {        /* "<>" if narrowed */
  1264.         vtputc('<');
  1265.         vtputc('>');
  1266.     } else {
  1267.         vtputc(lchar);
  1268.         vtputc(lchar);
  1269.     }
  1270.  
  1271.     n  = 4;
  1272.     strcpy(tline, " ");             /* Buffer name. */
  1273. #if     !WINDOW_MSWIN       /* we don't need that text, thanks to the
  1274.                    about box */
  1275.     strcat(tline, PROGNAME);
  1276.     strcat(tline, " ");
  1277.     strcat(tline, VERSION);
  1278.     strcat(tline, " ");
  1279. #endif
  1280.  
  1281.     /* display the time on the bottom most modeline if active */
  1282.     if (timeflag && wp->w_wndp == (WINDOW *)NULL) {
  1283.  
  1284.         /* get the current time/date string */
  1285.         getdtime(time);
  1286.         if (strcmp(time, "") != 0) {
  1287.  
  1288.             /* append the hour/min string */
  1289.             strcat(tline, "[");
  1290.             strcat(tline, time);
  1291.             strcat(tline, "] ");
  1292.             strcpy(lasttime, time);
  1293.         }
  1294.     }
  1295.  
  1296.     /* are we horizontally scrolled? */
  1297.     if (wp->w_fcol > 0) {
  1298.         strcat(tline, "[<");
  1299.         strcat(tline, int_asc(wp->w_fcol));
  1300.         strcat(tline, "]");
  1301.     }
  1302.  
  1303.     /* display the point position in buffer if on current modeline */
  1304.     if (posflag && wp == curwp) {
  1305.  
  1306.          strcat(tline, "L:");
  1307.          strcat(tline, int_asc(getlinenum(bp, wp->w_dotp)));
  1308.          strcat(tline, " C:");
  1309.          strcat(tline, int_asc(getccol(FALSE)));
  1310.          strcat(tline, " ");
  1311.     }
  1312.  
  1313.     /* display the modes */
  1314.     strcat(tline, "(");
  1315.     firstm = TRUE;
  1316.     for (i = 0; i < NUMMODES; i++)    /* add in the mode flags */
  1317.         if (wp->w_bufp->b_mode & (1 << i)) {
  1318.             if (firstm != TRUE)
  1319.                 strcat(tline, " ");
  1320.             firstm = FALSE;
  1321.             strcat(tline, modename[i]);
  1322.         }
  1323.     strcat(tline,") ");
  1324.  
  1325.     cp = &tline[0];
  1326.     while ((c = *cp++) != 0) {
  1327.         vtputc(c);
  1328.         ++n;
  1329.     }
  1330.  
  1331. #if    0
  1332.     vtputc(lchar);
  1333.     vtputc((wp->w_flag&WFCOLR) != 0  ? 'C' : lchar);
  1334.     vtputc((wp->w_flag&WFMODE) != 0  ? 'M' : lchar);
  1335.     vtputc((wp->w_flag&WFHARD) != 0  ? 'H' : lchar);
  1336.     vtputc((wp->w_flag&WFEDIT) != 0  ? 'E' : lchar);
  1337.     vtputc((wp->w_flag&WFMOVE) != 0  ? 'V' : lchar);
  1338.     vtputc((wp->w_flag&WFFORCE) != 0 ? 'F' : lchar);
  1339.     vtputc(lchar);
  1340.     n += 8;
  1341. #endif
  1342.  
  1343.     vtputc(lchar);
  1344.     vtputc(lchar);
  1345.     vtputc(' ');
  1346.     n += 3;
  1347.     cp = &bp->b_bname[0];
  1348.  
  1349.     while ((c = *cp++) != 0) {
  1350.         vtputc(c);
  1351.         ++n;
  1352.     }
  1353.  
  1354.     vtputc(' ');
  1355.     vtputc(lchar);
  1356.     vtputc(lchar);
  1357.     n += 3;
  1358.  
  1359.     if (bp->b_fname[0] != 0) {    /* File name. */
  1360.         vtputc(' ');
  1361.         ++n;
  1362.         cp = TEXT34;
  1363. /*                   "File: " */
  1364.  
  1365.         while ((c = *cp++) != 0) {
  1366.             vtputc(c);
  1367.             ++n;
  1368.         }
  1369.  
  1370.         cp = &bp->b_fname[0];
  1371.  
  1372.         while ((c = *cp++) != 0) {
  1373.             vtputc(c);
  1374.             ++n;
  1375.             }
  1376.  
  1377.         vtputc(' ');
  1378.         ++n;
  1379.     }
  1380.  
  1381.     while (n < term.t_ncol) {    /* Pad to full width. */
  1382.         vtputc(lchar);
  1383.         ++n;
  1384.     }
  1385. }
  1386.  
  1387. VOID PASCAL NEAR getdtime(ts)    /* get the current display time string */
  1388.  
  1389. char *ts;
  1390.  
  1391. {
  1392.     char buf[80];
  1393.  
  1394.     strcpy(buf, timeset());
  1395.     if (strcmp(buf, errorm) == 0) {
  1396.         *ts = 0;
  1397.         return;
  1398.     }
  1399.  
  1400.     buf[16] = 0;
  1401.     strcpy(ts, &buf[11]);
  1402.     return;
  1403. }
  1404.  
  1405. VOID PASCAL NEAR upmode()    /* update all the mode lines */
  1406.  
  1407. {
  1408.     register WINDOW *wp;
  1409. #if     WINDOW_MSWIN
  1410.     SCREEN  *sp;
  1411.  
  1412.     /* loop through all screens to update even partially hidden ones.
  1413.        Note that we process the "first" screen last */
  1414.     sp = first_screen;
  1415.     do {
  1416.         sp = sp->s_next_screen;
  1417.         if (sp == (SCREEN *)NULL) sp = first_screen;
  1418.         vtscreen (sp);
  1419.         wheadp = sp->s_first_window;
  1420. #endif
  1421.     wp = wheadp;
  1422.     while (wp != NULL) {
  1423.         wp->w_flag |= WFMODE;
  1424.         wp = wp->w_wndp;
  1425.     }
  1426. #if     WINDOW_MSWIN
  1427.     } while (sp != first_screen);
  1428. #endif
  1429. }
  1430.  
  1431. VOID PASCAL NEAR upwind()    /* force hard updates on all windows */
  1432.  
  1433. {
  1434.     register WINDOW *wp;
  1435. #if     WINDOW_MSWIN
  1436.     SCREEN  *sp;
  1437.  
  1438.     /* loop through all screens to update even partially hidden ones.
  1439.        Note that we process the "first" screen last */
  1440.     sp = first_screen;
  1441.     do {
  1442.         sp = sp->s_next_screen;
  1443.         if (sp == (SCREEN *)NULL) sp = first_screen;
  1444.         vtscreen (sp);
  1445.         wheadp = sp->s_first_window;
  1446. #endif
  1447.     wp = wheadp;
  1448.     while (wp != NULL) {
  1449.         wp->w_flag |= WFHARD|WFMODE;
  1450.         wp = wp->w_wndp;
  1451.     }
  1452. #if     WINDOW_MSWIN
  1453.     } while (sp != first_screen);
  1454. #endif
  1455. }
  1456.  
  1457. /*
  1458.  * Send a command to the terminal to move the hardware cursor to row "row"
  1459.  * and column "col". The row and column arguments are origin 0. Optimize out
  1460.  * random calls. Update "ttrow" and "ttcol".
  1461.  */
  1462. PASCAL NEAR movecursor(row, col)
  1463.  
  1464. int row, col;
  1465.  
  1466. {
  1467. #if     WINDOW_MSWIN
  1468.         if (row >= term.t_nrow) row = term.t_mrow;
  1469.             /* emphasize move into message line to avoid confusion with
  1470.            another, larger, screen */
  1471.         if (defferupdate) update (TRUE);
  1472.             /* a pending update could move the cursor somewhere else, so
  1473.            we make sure it can't happen */
  1474.     if (row!=ttrow || col!=ttcol || foulcursor) {
  1475. /* in MSWIN, the message line is a separate entity and a call to
  1476.    vtscreen after a movecursor calls might actually have "stolen" the
  1477.    cursor away from the message line! */
  1478.             foulcursor = FALSE;
  1479. #else
  1480.     if (row!=ttrow || col!=ttcol) {
  1481. #endif
  1482.         ttrow = row;
  1483.         ttcol = col;
  1484.         TTmove(row, col);
  1485.     }
  1486. }
  1487.  
  1488. /*
  1489.  * Erase the message line. This is a special routine because the message line
  1490.  * is not considered to be part of the virtual screen. It always works
  1491.  * immediately; the terminal buffer is flushed via a call to the flusher.
  1492.  */
  1493.  
  1494. VOID PASCAL NEAR mlferase()
  1495.  
  1496. {
  1497.     register int save_discmd;
  1498.  
  1499.     save_discmd = discmd;
  1500.     discmd = TRUE;
  1501.     mlerase();
  1502.     discmd = save_discmd;;
  1503. }
  1504.  
  1505. VOID PASCAL NEAR mlerase()
  1506.  
  1507. {
  1508.     int i;
  1509.     
  1510.     movecursor(term.t_nrow, 0);
  1511.     if (discmd == FALSE)
  1512.         return;
  1513.  
  1514. #if    COLOR
  1515.     TTforg(7);
  1516.     TTbacg(gbcolor);
  1517. #endif
  1518.  
  1519.     if (eolexist == TRUE)
  1520.         TTeeol();
  1521.     else {
  1522.         for (i = 0; i < term.t_ncol - 1; i++)
  1523.             TTputc(' ');
  1524.  
  1525.         /* force the move! */
  1526. /*        movecursor(term.t_nrow, 1);*/
  1527.         movecursor(term.t_nrow, 0);
  1528.     }
  1529.     TTflush();
  1530.     mpresf = FALSE;
  1531. }
  1532.  
  1533. /*
  1534.  * Write a message into the message line. Keep track of the physical cursor
  1535.  * position. A small class of printf like format items is handled. Assumes the
  1536.  * stack grows down; this assumption is made by the "+=" in the argument scan
  1537.  * loop. If  STACK_GROWS_UP  is set in estruct.h, then we'll assume that the
  1538.  * stack grows up and use "-=" instead of "+=". Set the "message line"
  1539.  *  flag TRUE.  Don't write beyond the end of the current terminal width.
  1540.  */
  1541.  
  1542. PASCAL NEAR mlout(c)
  1543.  
  1544. int c;    /* character to write */
  1545.  
  1546. {
  1547. #if WINDOW_MSWIN
  1548.         if (ttcol + 1 < NSTRING)
  1549. #else
  1550.     if (ttcol + 1 < term.t_ncol)
  1551. #endif
  1552.         TTputc(c);
  1553.     if (c != '\b')
  1554.         *lastptr++ = c;
  1555.     else if (lastptr > &lastmesg[0])
  1556.         --lastptr;
  1557. }
  1558.  
  1559. #if    VARARG
  1560. #if    VARG
  1561. CDECL NEAR mlwrite(va_alist)
  1562.  
  1563. va_dcl        /* variable argument list
  1564.             arg1 = format string
  1565.             arg2+ = arguments in that string */
  1566.  
  1567. {
  1568.     register int c;     /* current char in format string */
  1569.     register char *fmt;    /* ptr to format string */
  1570.     register va_list ap;    /* ptr to current data field */
  1571.     int arg_int;        /* integer argument */
  1572.     long arg_long;        /* long argument */
  1573.     char *arg_str;        /* string argument */
  1574.  
  1575.     /* if we are not currently echoing on the command line, abort this */
  1576.     if (discmd == FALSE)
  1577.         return;
  1578.  
  1579. #if    COLOR
  1580.     /* set up the proper colors for the command line */
  1581.     TTforg(7);
  1582.     TTbacg(gbcolor);
  1583. #endif
  1584.  
  1585.     /* point to the first argument */
  1586.     va_start(ap);
  1587.     fmt = va_arg(ap, char *);
  1588.  
  1589.     /* if we can not erase to end-of-line, do it manually */
  1590.     if (eolexist == FALSE) {
  1591.         mlerase();
  1592.         TTflush();
  1593.     }
  1594.  
  1595.     movecursor(term.t_nrow, 0);
  1596.      lastptr = &lastmesg[0];        /* setup to record message */
  1597.     while ((c = *fmt++) != 0) {
  1598.         if (c != '%') {
  1599.             mlout(c);
  1600.             ++ttcol;
  1601.         } else {
  1602.             c = *fmt++;
  1603.             switch (c) {
  1604.                 case 'd':
  1605.                     arg_int = va_arg(ap, int);
  1606.                     mlputi(arg_int, 10);
  1607.                     break;
  1608.  
  1609.                 case 'o':
  1610.                     arg_int = va_arg(ap, int);
  1611.                     mlputi(arg_int, 8);
  1612.                     break;
  1613.  
  1614.                 case 'x':
  1615.                     arg_int = va_arg(ap, int);
  1616.                     mlputi(arg_int, 16);
  1617.                     break;
  1618.  
  1619.                 case 'D':
  1620.                     arg_long = va_arg(ap, long);
  1621.                     mlputli(arg_long, 10);
  1622.                     break;
  1623.  
  1624.                 case 's':
  1625.                     arg_str = va_arg(ap, char *);
  1626.                     mlputs(arg_str);
  1627.                     break;
  1628.  
  1629.                 case 'f':
  1630.                     arg_int = va_arg(ap, int);
  1631.                     mlputf(arg_int);
  1632.                     break;
  1633.  
  1634.                 default:
  1635.                     mlout(c);
  1636.                     ++ttcol;
  1637.             }
  1638.         }
  1639.     }
  1640.  
  1641.     /* if we can, erase to the end of screen */
  1642.     if (eolexist == TRUE)
  1643.         TTeeol();
  1644.     TTflush();
  1645.     mpresf = TRUE;
  1646.     *lastptr = 0;    /* terminate lastmesg[] */
  1647.     va_end(ap);
  1648. }
  1649. #else
  1650. CDECL NEAR mlwrite(char *fmt, ...)
  1651. /* char * fmt;*/
  1652.  
  1653.         /* variable argument list
  1654.             arg1 = format string
  1655.             arg2+ = arguments in that string */
  1656.  
  1657. {
  1658.     register int c;     /* current char in format string */
  1659.     va_list ap;        /* ptr to current data field */
  1660.     int arg_int;        /* integer argument */
  1661.     long arg_long;        /* long argument */
  1662.     char *arg_str;        /* string argument */
  1663.  
  1664.     /* if we are not currently echoing on the command line, abort this */
  1665.     if (discmd == FALSE)
  1666.         return;
  1667.  
  1668. #if    COLOR
  1669.     /* set up the proper colors for the command line */
  1670.     TTforg(7);
  1671.     TTbacg(gbcolor);
  1672. #endif
  1673.  
  1674.     /* point to the first argument */
  1675.     va_start(ap, fmt);
  1676.  
  1677.     /* if we can not erase to end-of-line, do it manually */
  1678.     if (eolexist == FALSE) {
  1679.         mlerase();
  1680.         TTflush();
  1681.     }
  1682.  
  1683.     movecursor(term.t_nrow, 0);
  1684.      lastptr = &lastmesg[0];        /* setup to record message */
  1685.     while ((c = *fmt++) != 0) {
  1686.         if (c != '%') {
  1687.             mlout(c);
  1688.             ++ttcol;
  1689.         } else {
  1690.             c = *fmt++;
  1691.             switch (c) {
  1692.                 case 'd':
  1693.                     arg_int = va_arg(ap, int);
  1694.                     mlputi(arg_int, 10);
  1695.                     break;
  1696.  
  1697.                 case 'o':
  1698.                     arg_int = va_arg(ap, int);
  1699.                     mlputi(arg_int, 8);
  1700.                     break;
  1701.  
  1702.                 case 'x':
  1703.                     arg_int = va_arg(ap, int);
  1704.                     mlputi(arg_int, 16);
  1705.                     break;
  1706.  
  1707.                 case 'D':
  1708.                     arg_long = va_arg(ap, long);
  1709.                     mlputli(arg_long, 10);
  1710.                     break;
  1711.  
  1712.                 case 's':
  1713.                     arg_str = va_arg(ap, char *);
  1714.                     mlputs(arg_str);
  1715.                     break;
  1716.  
  1717.                 case 'f':
  1718.                     arg_int = va_arg(ap, int);
  1719.                     mlputf(arg_int);
  1720.                     break;
  1721.  
  1722.                 default:
  1723.                     mlout(c);
  1724.                     ++ttcol;
  1725.             }
  1726.         }
  1727.     }
  1728.  
  1729.     /* if we can, erase to the end of screen */
  1730.     if (eolexist == TRUE)
  1731.         TTeeol();
  1732.     TTflush();
  1733.     mpresf = TRUE;
  1734.     *lastptr = 0;    /* terminate lastmesg[] */
  1735.     va_end(ap);
  1736. }
  1737. #endif
  1738. #else
  1739.  
  1740. #if    STACK_GROWS_UP
  1741. #define    ADJUST(ptr, dtype)    ptr -= sizeof(dtype)
  1742. #else
  1743. #define    ADJUST(ptr, dtype)    ptr += sizeof(dtype)
  1744. #endif
  1745.  
  1746. CDECL NEAR mlwrite(fmt)
  1747.  
  1748. char *fmt;    /* format string for output */
  1749.  
  1750. {
  1751.     register int c;     /* current char in format string */
  1752.     register char *ap;    /* ptr to current data field */
  1753.  
  1754.     /* if we are not currently echoing on the command line, abort this */
  1755.     if (discmd == FALSE)
  1756.         return;
  1757.  
  1758. #if    COLOR
  1759.     /* set up the proper colors for the command line */
  1760.     TTforg(7);
  1761.     TTbacg(gbcolor);
  1762. #endif
  1763.  
  1764.     /* point to the first argument */
  1765.     ap = &fmt;
  1766.     ADJUST(ap, char *);
  1767.  
  1768.     /* if we can not erase to end-of-line, do it manually */
  1769.     if (eolexist == FALSE) {
  1770.         mlerase();
  1771.         TTflush();
  1772.     }
  1773.  
  1774.     movecursor(term.t_nrow, 0);
  1775.      lastptr = &lastmesg[0];        /* setup to record message */
  1776.     while ((c = *fmt++) != 0) {
  1777.         if (c != '%') {
  1778.             mlout(c);
  1779.             ++ttcol;
  1780.         } else {
  1781.             c = *fmt++;
  1782.             switch (c) {
  1783.                 case 'd':
  1784.                     mlputi(*(int *)ap, 10);
  1785.                             ADJUST(ap, int);
  1786.                     break;
  1787.  
  1788.                 case 'o':
  1789.                     mlputi(*(int *)ap,  8);
  1790.                     ADJUST(ap, int);
  1791.                     break;
  1792.  
  1793.                 case 'x':
  1794.                     mlputi(*(int *)ap, 16);
  1795.                     ADJUST(ap, int);
  1796.                     break;
  1797.  
  1798.                 case 'D':
  1799.                     mlputli(*(long *)ap, 10);
  1800.                     ADJUST(ap, long);
  1801.                     break;
  1802.  
  1803.                 case 's':
  1804.                     mlputs(*(char **)ap);
  1805.                     ADJUST(ap, char *);
  1806.                     break;
  1807.  
  1808.                 case 'f':
  1809.                     mlputf(*(int *)ap);
  1810.                     ADJUST(ap, int);
  1811.                     break;
  1812.  
  1813.                 default:
  1814.                     mlout(c);
  1815.                     ++ttcol;
  1816.             }
  1817.         }
  1818.     }
  1819.  
  1820.     /* if we can, erase to the end of screen */
  1821.     if (eolexist == TRUE)
  1822.         TTeeol();
  1823.     TTflush();
  1824.     mpresf = TRUE;
  1825.     *lastptr = 0;    /* terminate lastmesg[] */
  1826. }
  1827. #endif
  1828.  
  1829. #if     !WINDOW_MSWIN
  1830. /* display a serious error message (like "out of memory"). This is
  1831.    replaced by a system-specific function when a multitasking system
  1832.    that does not like these kind of errors is used, so that the user can
  1833.    be offered to abort the application */
  1834.  
  1835. PASCAL NEAR mlabort(s)
  1836. char *s;
  1837. {
  1838.     mlforce(s);
  1839. }
  1840. #endif
  1841. /*    Force a string out to the message line regardless of the
  1842.     current $discmd setting. This is needed when $debug is TRUE
  1843.     and for the write-message and clear-message-line commands
  1844. */
  1845.  
  1846. PASCAL NEAR mlforce(s)
  1847.  
  1848. char *s;    /* string to force out */
  1849.  
  1850. {
  1851.     register int oldcmd;    /* original command display flag */
  1852.  
  1853.     oldcmd = discmd;    /* save the discmd value */
  1854.     discmd = TRUE;        /* and turn display on */
  1855.     mlwrite(s);        /* write the string out */
  1856.     discmd = oldcmd;    /* and restore the original setting */
  1857. }
  1858.  
  1859. /*
  1860.  * Write out a string. Update the physical cursor position. This assumes that
  1861.  * the characters in the string all have width "1"; if this is not the case
  1862.  * things will get screwed up a little.
  1863.  */
  1864.  
  1865. PASCAL NEAR mlputs(s)
  1866.  
  1867. char *s;
  1868.  
  1869. {
  1870.     register int c;
  1871.  
  1872.     while ((c = *s++) != 0) {
  1873.         mlout(c);
  1874.         ++ttcol;
  1875.     }
  1876. }
  1877.  
  1878. /*
  1879.  * Write out an integer, in the specified radix. Update the physical cursor
  1880.  * position.
  1881.  */
  1882. PASCAL NEAR mlputi(i, r)
  1883.  
  1884. int i, r;
  1885.  
  1886.     {
  1887.     register int q;
  1888.     static char hexdigits[] = "0123456789ABCDEF";
  1889.  
  1890.     if (i < 0)
  1891.     {
  1892.     i = -i;
  1893.     mlout('-');
  1894.     }
  1895.  
  1896.     q = i/r;
  1897.  
  1898.     if (q != 0)
  1899.     mlputi(q, r);
  1900.  
  1901.     mlout(hexdigits[i%r]);
  1902.     ++ttcol;
  1903.     }
  1904.  
  1905. /*
  1906.  * do the same except as a long integer.
  1907.  */
  1908. PASCAL NEAR mlputli(l, r)
  1909.  
  1910. long l;
  1911. int r;
  1912.  
  1913.     {
  1914.     register long q;
  1915.  
  1916.     if (l < 0)
  1917.     {
  1918.     l = -l;
  1919.     mlout('-');
  1920.     }
  1921.  
  1922.     q = l/r;
  1923.  
  1924.     if (q != 0)
  1925.     mlputli(q, r);
  1926.  
  1927.     mlout((int)(l%r)+'0');
  1928.     ++ttcol;
  1929.     }
  1930.  
  1931. /*
  1932.  *    write out a scaled integer with two decimal places
  1933.  */
  1934.  
  1935. PASCAL NEAR mlputf(s)
  1936.  
  1937. int s;    /* scaled integer to output */
  1938.  
  1939. {
  1940.     int i;    /* integer portion of number */
  1941.     int f;    /* fractional portion of number */
  1942.  
  1943.     /* break it up */
  1944.     i = s / 100;
  1945.     f = s % 100;
  1946.  
  1947.     /* send out the integer portion */
  1948.     mlputi(i, 10);
  1949.     mlout('.');
  1950.     mlout((f / 10) + '0');
  1951.     mlout((f % 10) + '0');
  1952.     ttcol += 3;
  1953. }       
  1954.  
  1955. #if RAINBOW
  1956.  
  1957. PASCAL NEAR putline(row, col, buf)
  1958.     int row, col;
  1959.     char buf[];
  1960.     {
  1961.     int n;
  1962.  
  1963.     n = strlen(buf);
  1964.     if (col + n - 1 > term.t_ncol)
  1965.     n = term.t_ncol - col + 1;
  1966.     Put_Data(row, col, n, buf);
  1967.     }
  1968. #endif
  1969.  
  1970.